<?php

namespace App\Models\Order;

use App\Models\Common\CommonModel;
use App\Models\Repair\RepairFactory;
use App\Models\User\ChengBao;
use App\Models\User\User;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Order extends CommonModel
{
    use HasFactory;

    protected $table = 'order';

    public function getRecords(array $conditions) {
        $records = $this->newQuery();
        //dd($conditions);
        if(!empty($conditions['userId'])){
            $records = $records->where('user_id',$conditions['userId']);
        }
        if(!empty($conditions['caseNumber'])){
            $records = $records->where('case_number',$conditions['caseNumber']);
        }
        if(!empty($conditions['carNumber'])){
            $records = $records->where('car_number',$conditions['carNumber']);
        }
        if(!empty($conditions['baoXianName'])){
            $records = $records->where('bao_xian_name','like','%'.$conditions['baoXianName'].'%');
        }
        if(!empty($conditions['repairFactoryId'])){
            $records = $records->where('repair_factory_id',$conditions['repairFactoryId']);
        }
        if(!empty($conditions['useWay'])){
            if($conditions['useWay']==1){
                $records = $records->where('repair_factory_id',0);
            }else{
                $records = $records->where('repair_factory_id','!=',0);
            }
        }
        if(isset($conditions['dingSunPrice'])){
            if(is_array($conditions['dingSunPrice'])){
                $conditions['dingSunPrice'] = array_filter($conditions['dingSunPrice']);
                if(count($conditions['dingSunPrice'])){
                    $records->whereBetween('ding_sun_price',$conditions['dingSunPrice']);
                }
            }
            else{
                $records = $records->where('ding_sun_price',$conditions['dingSunPrice']);
            }
        }
        if(!empty($conditions['status'])){
            if(is_array($conditions['status'])){
                $records->whereIn('status',$conditions['status']);
            }
            else{
                $records = $records->where('status',$conditions['status']);
            }
        }
        if(!empty($conditions['createdAt'])){
            if(is_array($conditions['createdAt'])){
                $records->whereBetween('created_at',$conditions['createdAt']);
            }
            else{
                $records = $records->where('created_at',$conditions['createdAt']);
            }
        }
        if(!empty($conditions['chuXianDate'])){
            if(is_array($conditions['chuXianDate'])){
                $records->whereBetween('chu_xian_date',$conditions['chuXianDate']);
            }
            else{
                $records = $records->where('chu_xian_date',$conditions['chuXianDate']);
            }
        }
        return $records->orderByDesc('id');
    }

    public function getRepairFactory() {
        return $this->hasOne(RepairFactory::class,'id','repair_factory_id')->withDefault(['name'=>'']);
    }

    public function owner() {
        return $this->hasOne(User::class,'id','user_id');
    }

    public function compareOcr() :bool{

        $ocr = Ocr::where('id',$this->ocr_id)->first();

        if(empty($ocr))return false;

        $reasons = [];
        //dump($this->ding_sun_price,$ocr->ding_sun_price);
        if($this->case_number != $ocr->case_number){
            //$reasons[] = '立案号错误！';
        }
        if($this->bao_xian_name != $ocr->bao_xian_name){
            $reasons[] = '被保险人姓名错误！';
        }
        if(date('Y-m-d',strtotime($ocr->chu_xian_date))!=date('Y-m-d',strtotime($this->chu_xian_date))){
            //$reasons[] = '出险时间错误！';
        }
        if($this->ding_sun_price != $ocr->ding_sun_price){
            $reasons[] = '定损金额错误！';
        }
        if($this->car_number != $ocr->car_number){
            $reasons[] = '车牌号错误！';
        }
        if($this->car_jia_number != $ocr->car_jia_number){
            $reasons[] = '车架号错误！';
        }
        //dd($reasons);
        if(count($reasons)>0){
            $this->ocr_reason  = implode(' | ', $reasons);
            $this->save();
        }
        return true;
    }

    public function getCarJiaNumberOfChengBao() {
        return ChengBao::where(['beibao_name'=>$this->bao_xian_name,'car_number'=>$this->car_number])->first()->car_jia_number??'';
    }

}
